// order.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        "condition": true,
        currentIndex: 0,
        "code": 1,
        "success": "ok",
        activeB: false,
        // carts: [], // 购物车列表
        hasList: false, // 列表是否有数据
        totalPrice: 0, // 总价，初始为0
        selectAllStatus: true, // 全选状态，默认全选
        hasList: true, // 既然有数据了，那设为true吧
        pageIndex: 2,
        flag: true,
        items: [{
            contain: '我不想买了',
            checked: 'true'
        }, {
            contain: '信息填写错误'
        }, {
            contain: '卖家缺货'
        }, {
            contain: '其他问题'
        }],
        titl: ['全部', '待付款', '待发货', '待收货']
    },
    onAside: function(options) {
        //下标
        var index = options.currentTarget.dataset.index;
        console.log(index)
        this.setData({
            currentIndex: index
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/list.do',
            data: {
                openId: app.globalData.openid
            },
            success: function(res) {
                console.log(res);
                that.setData({
                    ordersList: res.data.ordersList,
                    dfkList: res.data.dfkList,
                    dfhList: res.data.dfhList,
                    dshList: res.data.dshList
                })
            }
        })
        this.setData({
            // categoryData: categoryData.default.data
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindproductInfo: function() {
        wx.navigateTo({
            url: '../productDetail/productDetail'
        })
    },

    getTotalPrice(e) {
        let top = this.data.top; // 获取购物车列表
        console.log(top)
        let total = 0;
        let Num = 0;
        for (let i = 0; i < top.length; i++) { // 循环列表得到每个数据
            for (let j = 0; j < top[i].carts.length; j++) {
                if (top[i].carts[j].selected) { // 判断选中才会计算价格
                    total += top[i].carts[j].num * top[i].carts[j].price; // 所有价格加起来
                    Num += top[i].carts[j].num; //所有商品数量加起来
                }
            }
        }
        this.setData({ // 最后赋值到data中渲染到页面
            top: top,
            totalPrice: total.toFixed(2),
            Num: Num
        });
    },
    selectList(e) {
        const inde = e.currentTarget.dataset.index; // 获取data- 传进来的index
        let id = e.currentTarget.dataset.id; //二维数组需要一个id来定位
        let top = this.data.top; //数据的获取
        top[id].carts[inde].selected = !top[id].carts[inde].selected;
        let num = 0;



        this.setData({
            top: top
        });
        this.getTotalPrice(); // 重新获取总价
    },


    bindShowToast: function(e) {
        let activeB = this.data.activeB;
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/delete.do',
            data: {
                openId: app.globalData.openid,
                ordersId: e.currentTarget.id
            },
            success: function(res) {
                console.log(res)

            }
        })
        activeB = !activeB;
        this.setData({
            activeB: activeB
        })
    },
    defaultTapOne: function() {
        let activeB = this.data.activeB;
        activeB = !activeB;
        this.setData({
            activeB: activeB
        })
        wx.redirectTo({
            url: "order",
        })
    },
    defaultTapTwo: function() {
        let activeB = this.data.activeB;
        activeB = !activeB;
        this.setData({
            activeB: activeB
        })
    },
    scrolltolower: function() {

        var that = this
        var flag = that.data.flag;
        var pageIndex = that.data.pageIndex;
        var categoryId = that.data.categoryId;
        if (flag) {
            that.setData({
                flag: false
            })
            wx.request({
                url: app.data.baseUrl + '/AibraUsb/orders/pageList.do',
                data: {
                    openId: app.globalData.openid,
                    pageIndex: that.data.pageIndex
                },
                success: function(res) {
                    // console.log('1111', res)
                    var ordersList1 = res.data.ordersList;
                    var ordersList2 = that.data.ordersList;
                    if (ordersList1.length == 0) {
                        console.log("数据完事")
                        return
                    }
                    ordersList2 = ordersList2.concat(ordersList1)

                    pageIndex = ++pageIndex;
                    that.setData({
                        ordersList: ordersList2,
                        pageIndex: pageIndex,
                        flag: true
                    })
                },
                fail: function() {
                    console.log("失败")
                }
            })
        }

        // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    },

    phoneNumTap1: function(e) {
        var that = this;

        // 提示呼叫号码还是将号码添加到手机通讯录
        wx.showActionSheet({
            itemList: ['呼叫', '添加联系人'],
            success: function(res) {
                if (res.tapIndex === 0) {
                    // 呼叫号码
                    wx.makePhoneCall({
                        phoneNumber: that.data.ordersList[e.currentTarget.dataset.index][9],
                    })
                } else if (res.tapIndex == 1) {
                    // 添加到手机通讯录
                    wx.addPhoneContact({
                        firstName: 'test', //联系人姓名
                        mobilePhoneNumber: that.data.phoneNum, //联系人手机号
                    })
                }
            }
        })
    },
    phoneNumTap2: function(e) {
        var that = this;

        // 提示呼叫号码还是将号码添加到手机通讯录
        wx.showActionSheet({
            itemList: ['呼叫', '添加联系人'],
            success: function(res) {
                if (res.tapIndex === 0) {
                    // 呼叫号码
                    wx.makePhoneCall({
                        phoneNumber: that.data.dfkList[e.currentTarget.dataset.index][9],
                    })
                } else if (res.tapIndex == 1) {
                    // 添加到手机通讯录
                    wx.addPhoneContact({
                        firstName: 'test', //联系人姓名
                        mobilePhoneNumber: that.data.phoneNum, //联系人手机号
                    })
                }
            }
        })
    },
    phoneNumTap3: function(e) {
        var that = this;

        // 提示呼叫号码还是将号码添加到手机通讯录
        wx.showActionSheet({
            itemList: ['呼叫', '添加联系人'],
            success: function(res) {
                if (res.tapIndex === 0) {
                    // 呼叫号码
                    wx.makePhoneCall({
                        phoneNumber: that.data.dfhList[e.currentTarget.dataset.index][9],
                    })
                } else if (res.tapIndex == 1) {
                    // 添加到手机通讯录
                    wx.addPhoneContact({
                        firstName: 'test', //联系人姓名
                        mobilePhoneNumber: that.data.phoneNum, //联系人手机号
                    })
                }
            }
        })
    },
    phoneNumTap4: function(e) {
        var that = this;

        // 提示呼叫号码还是将号码添加到手机通讯录
        wx.showActionSheet({
            itemList: ['呼叫', '添加联系人'],
            success: function(res) {
                if (res.tapIndex === 0) {
                    // 呼叫号码
                    wx.makePhoneCall({
                        phoneNumber: that.data.dshList[e.currentTarget.dataset.index][9],
                    })
                } else if (res.tapIndex == 1) {
                    // 添加到手机通讯录
                    wx.addPhoneContact({
                        firstName: 'test', //联系人姓名
                        mobilePhoneNumber: that.data.phoneNum, //联系人手机号
                    })
                }
            }
        })
    },

    confirm: function(e) {
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/update.do',
            data: {
                openId: app.globalData.openid,
                ordersId: e.currentTarget.id
            },
            success: function(res) {

                wx.showToast({
                    title: '确认收货'
                })
                wx.redirectTo({
                    url: '../personal'
                })
            }
        })
    },
    // 支付方法
    HelloWorld: function(e) {
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/orderPay.do',
            data: {
                openId: app.globalData.openid,
                ordersId: e.currentTarget.id
            },
            success: function(res) {
                console.log(res)
                wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,

                    agent: res.data.agent,
                    appId: res.data.appId,
                    packageValue: res.data.packageValue,
                    sendUrl: res.data.sendUrl,
                    success: function(res) {
                        console.log(res)
                    }
                })
            }
        })
    },
    bindIndex: function() {
        wx.switchTab({
            url: '../../index/index'
        })
    },
    logistics: function(e) {
        console.log(e)
        wx.navigateTo({
            url: 'logistics/logistics?streamName=' + e.currentTarget.dataset.streamname + '&streamNo=' + e.currentTarget.dataset.streamno
        })
    }
})